package com.mouse.damo.lw;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

public class Test {
    public static void main(String[] args) throws Exception {
        Document document = Jsoup.parse(new File("C:\\Users\\Jim_Biao\\Documents\\WinCHM Projects\\Project1_output\\SingleHTML\\乐玩插件接口说明.htm"), "utf-8");
        Elements body = document.getElementsByTag("h3");
        List<String> errorList = new ArrayList<>();
        List<String> paramList = new ArrayList<>();
        List<String> result = new ArrayList<>();
        for (Element element : body) {
            System.out.println(element.text());
            Element next = element.nextElementSibling();
            StringBuffer sb = new StringBuffer();
            while (next != null && !next.tagName().equalsIgnoreCase("h3")) {
                String text = next.text();
//                System.out.println(text);
                next = next.nextElementSibling();
                sb.append(text.replace("\n", ""));
            }
            String name = element.text().replaceAll(".+[a-zA-Z]+.", "");
            String method = element.text().replaceAll(".*[ 　　]([a-zA-Z1-9]+)[ 　　].*", "$1");
            System.out.println(name + "++++++" + method);
            String regex = ".+[^a-zA-Z]([a-zA-Z]+[ 　　]" + method + "\\([^\\)]+\\))[  ]*参数定义:(.+)返回值:.+";
            String regex1 = ".+函数原型:(.+) 参数定义:(.+)返回值:.+";
            String regex2 = ".+函数原型:(.+) 返回值:.+";
            String function = null;
            String value = null;
            if (sb.toString().matches(regex)) {
                function = sb.toString().replaceAll(regex, "$1");
                value = sb.toString().replaceAll(regex, "$2");
            } else if (sb.toString().matches(regex1)) {
                function = sb.toString().replaceAll(regex1, "$1");
                value = sb.toString().replaceAll(regex1, "$2");
            } else if (sb.toString().matches(regex2)) {
                function = sb.toString().replaceAll(regex2, "$1");
                value = "";
            }
//            List<String> ignore = Arrays.asList("EnableSwitchWindow", "FindPic(", "FetchLattice", "ToAddr"
//                    , "GetModuleBaseAddrr","WriteUniversal","ReadInt","ReadStringEx","ReadDataEx","VirtualAllocEx");
//            if (ignore.stream().anyMatch(s -> element.text().contains(s))) continue;
            if (value == null) {
                errorList.add(element.text());
                continue;
            }
            System.out.println(function.trim());
            if (value.trim().equals("")) {
                result.add("//"+name + "\n" +function.trim()+";");
                continue;
            }
            System.out.println(value);
            String params = function.replaceAll(".+\\(", "").replace(")", "");
            String[] split = params.split(",");
            List<String> dataList = new ArrayList<>();
            boolean fail = false;
            for (String s : split) {
                String type = value.replaceAll(".*[^a-zA-Z]+" + s + "[ 　](.{2,5})[：:].+", "$1");
                if (value.equalsIgnoreCase(type)) {
                    paramList.add(element.text() + "+++" + s);
                    fail = true;
                    continue;
                }
                System.out.print(type + " ");
                dataList.add(getByType(type) + " " + s);
            }
            System.out.println();
            System.out.println("++++++++++");
            if (fail) {
                result.add("//"+name + "\n" +function.trim()+";");
            } else {
                result.add("//"+name + "\n" + function.trim().replace("strring", "string").replace("string", "String").
                        replaceAll("\\(.*", "") + "(" + String.join(",", dataList) + ");");
            }
        }
        System.out.println(errorList);
        for (String s : result) {
            System.out.println(s);
        }
    }

    private static String getByType(String name) {
        name = name.replaceAll("：|:", "").trim();
        String result = null;
        switch (name) {
            case "整形数":
                result = "int";
                break;
            case "整数型":
                result = "int";
                break;
            case "字符串":
                result = "String";
                break;
            case "文本型":
                result = "String";
                break;
            case "小数型":
                result = "double";
                break;
            default:
                break;
        }
        return result;
    }

}
